package com.water.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.springframework.util.Assert;

import com.intranet.sys.domain.Menu;
import com.intranet.sys.domain.User;
import com.water.cons.CommonConstant;

/**
 * 所有Controller的基类
 * 
 * @author whl
 * 
 */
public class BaseController {

	protected static final String ERROR_MSG_KEY = "errorMsg";

	/**
	 * 获取保存在Session中的用户对象
	 * 
	 * @param request
	 * @return
	 */
	protected User getSessionUser(HttpServletRequest request) {
		return (User) request.getSession().getAttribute(
				CommonConstant.USER_CONTEXT);
	}

	/**
	 * 保存用户对象到Session中
	 * 
	 * @param request
	 * @param user
	 */
	protected void setSessionUser(HttpServletRequest request, User user) {
		request.getSession().setAttribute(CommonConstant.USER_CONTEXT, user);
	}

	/**
	 * 保存用户的皮肤
	 * 
	 * @param request
	 * @param user
	 */
	protected void setSessionTheme(HttpServletRequest request, String theme) {
		request.getSession().setAttribute("theme", theme);
	}
	
	/**
	 * 左侧菜单
	 * 
	 * @param request
	 * @param user
	 */
	protected void setSessionLeftMenus(HttpServletRequest request,List<Menu> menus) {
		request.getSession().setAttribute("leftMenus", menus);
	}
	
	/**
	 * 保存用户名
	 * 
	 * @param request
	 * @param user
	 */
	protected void setSessionUsername(HttpServletRequest request, String username) {
		request.getSession().setAttribute(CommonConstant.USERNAME, username);
	}

	/**
	 * 获取基于应用程序的url绝对路径
	 * 
	 * @param request
	 * @param url
	 *            以"/"打头的URL地址
	 * @return 基于应用程序的url绝对路径
	 */
	public final String getAppbaseUrl(HttpServletRequest request, String url) {
		Assert.hasLength(url, "url不能为空");
		Assert.isTrue(url.startsWith("/"), "必须以/打头");
		return request.getContextPath() + url;
	}

	/**
	 * 获取项目相对路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getPath(HttpServletRequest request) {
		String basePath = request.getContextPath();
		return basePath;
	}
	/**
	 * 获取项目绝对路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getServletContext(HttpServletRequest request) {
		String servletContext = request.getSession().getServletContext().toString();
		return servletContext;
	}
}
